<script setup lang='ts'>
import { TUIKit } from './TUIKit';
import { TUICallKit } from '@tencentcloud/call-uikit-vue';
import { ref, onMounted } from 'vue';
import { messageApi } from '@/api/message';

// 从环境变量或配置文件获取应用信息
const SDKAppID = ref(); // 替换为你的 SDKAppID
const userID = ref();
const userSig = ref();

// 从服务端获取用户信息和签名
async function fetchUserCredentials() {
  try {
    // 调用后端API获取用户ID和签名
    const response = await messageApi.getTencentCredentials();
    userID.value = response.userID;
    userSig.value = response.userSig;
    SDKAppID.value = response.SDKAppID

    console.log('获取腾讯云通信凭证成功');
  } catch (error) {
    console.error('获取腾讯云通信凭证失败:', error);
  }
}

onMounted(async () => {
  await fetchUserCredentials();
});
</script>
<template>
  <div class="chat-page page-container">
    <div class="content-container">
      <TUIKit v-if="userID && userSig" :SDKAppID="SDKAppID" :userID="userID" :userSig="userSig" style="height: 100%;" />
      <TUICallKit v-if="userID && userSig" class="callkit-container" :allowedMinimized="true"
        :allowedFullScreen="false" />
      <div v-else class="loading-container">
        <p>正在加载通信组件...</p>
      </div>
    </div>
  </div>
</template>
<style lang='scss' scoped>
::v-deep.TUIKit {
  height: 100% !important;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  font-size: 16px;
  color: #666;
}
</style>
